/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.jdbc.configuration;

import java.time.LocalDateTime;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.iiifi.kite.configuration.KiteProperties;
import com.iiifi.kite.jdbc.mybatis.KiteMapperScan;
import com.iiifi.kite.jdbc.utils.AccountUtils;
import com.iiifi.kite.sequence.IdGeneratorService;

import lombok.AllArgsConstructor;

/**
 * mybatis-plus 配置
 *
 * @author kite@iiifi.com 花朝
 */
@Configuration
@AllArgsConstructor
@KiteMapperScan(basePackages = "${kite.data.mybatis.base-packages}")
public class MybatisPlusAutoConfiguration {

    private final KiteProperties kiteProperties;

    private final IdGeneratorService idGeneratorService;

    @Component
    public class KiteMetaObjectHandler implements MetaObjectHandler {

        final String ID = "id";
        final String CREATED_AT = "createdAt";
        final String CREATED_BY = "createdBy";
        final String UPDATED_AT = "updatedAt";
        final String UPDATED_BY = "updatedBy";
        final String IS_DELETED = "isDeleted";

        @Override
        public void insertFill(MetaObject metaObject) {
            String account = AccountUtils.getXAccount(kiteProperties.getHeaders().getAccountId());
            LocalDateTime currentTime = LocalDateTime.now();
            if (this.getFieldValByName(ID, metaObject) == null) {
                this.setFieldValByName(ID, idGeneratorService.getId(), metaObject);
            }
            if (this.getFieldValByName(CREATED_AT, metaObject) == null) {
                this.setFieldValByName(CREATED_AT, currentTime, metaObject);
            }
            if (this.getFieldValByName(CREATED_BY, metaObject) == null) {
                this.setFieldValByName(CREATED_BY, account, metaObject);
            }
            if (this.getFieldValByName(UPDATED_AT, metaObject) == null) {
                this.setFieldValByName(UPDATED_AT, currentTime, metaObject);
            }
            if (this.getFieldValByName(UPDATED_BY, metaObject) == null) {
                this.setFieldValByName(UPDATED_BY, account, metaObject);
            }
            this.setFieldValByName(IS_DELETED, Boolean.FALSE, metaObject);
        }

        @Override
        public void updateFill(MetaObject metaObject) {
            String account = AccountUtils.getXAccount(kiteProperties.getHeaders().getAccountId());
            LocalDateTime currentTime = LocalDateTime.now();
            if (this.getFieldValByName(UPDATED_AT, metaObject) == null) {
                this.setFieldValByName(UPDATED_AT, currentTime, metaObject);
            }
            if (this.getFieldValByName(UPDATED_BY, metaObject) == null) {
                this.setFieldValByName(UPDATED_BY, account, metaObject);
            }
        }
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}
